<?php

include_once 'application/functions.php';


/**
 * Controller class.
 */
class Web_Controller extends Psa_Plugin_Controller{


	/*
	 * Reference to result object
	 */
	protected $psa_result;
	
	
	/*
	 * Controller main method
	 */
	function psa_main($url_args){

		// global config arrays
		global $PSA_CFG, $CFG;
		
		// make action function name from url
		$action_method = trim($url_args[0]);
		
		// put reference to this controller object into registry
		Psa_Registry::get_instance()->Web_Controller = $this;
		
		// reference to result object
		$this->psa_result = Psa_Result::get_instance();
		
		// put some default values in result object that you will need on every request
		// regardless of is user authorized or not
		if(@$_SERVER["HTTP_X_REQUESTED_WITH"] == 'XMLHttpRequest')
			$this->psa_result->ajax_request = 1;
		else
			$this->psa_result->ajax_request = 0;
		$this->psa_result->basedir_web = $PSA_CFG['folders']['basedir_web'];
		$this->psa_result->html_page_title = $CFG['app_name'];
		$this->psa_result->app_version = $CFG['app_ver'];
		
		// include all action methods
		include_once 'application/controllers/Web_Controller_Actions.php';

		// make new object with action methods
		$web_actions = new Web_Controller_Actions($this);
		
		// check if we have defined requested action, if not call index action by default
		if(!method_exists($web_actions, $action_method))
			$action_method = 'index';

		// call action and if Unauthorized_Access_Exception raised call login action 
		try{
			$web_actions->$action_method($url_args);
		}
		catch (Unauthorized_Access_Exception $e){
			$web_actions->login($url_args);
		}
	}

	
	/**
	 * Runs plugins.
	 * @param array $run_plugins argument for psa_run_plugins() function.
	 */
	function run_plugins($run_plugins){
		
		// first run model plugins and catch all exceptions
		try {
			if(isset($run_plugins['model']))
				psa_run_plugins($run_plugins['model']);
		}
		// if any exception is raised call error page
		catch (Exception $e){
			$run_plugins = array();
			$run_plugins['view']['Main_View']['uncaught_exception_redirect'] = array($e);
		}
		
		// After model plugins are called, assign all results from models (every property in $this->psa_result object)
		// to smarty ($psa_smarty) object. I use smarty template engine here.
		$this->smarty_assign_object_properties($this->psa_result);
		
		// run view plugins at the end
		if(isset($run_plugins['view']))
			psa_run_plugins($run_plugins['view']);
	}
	
	
	/**
	 * Checks login and sets some values to registry object and session.
	 * @param string $session_id
	 */
	function check_login(){
		
		global $CFG;
		
		// new user object
		$user = new Psa_User();
		
		// Check if we have user object stored in session.
		// That means that user is already authorized.
		if(!$user->session_restore()){
			
			// check if login data are sent from login form
			if(isset($_POST['login_user']) && isset($_POST['login_pass'])){
				$user->username = $_POST['login_user'];
				$user->password = $_POST['login_pass'];
				if(!$user->authorize()){
					$this->psa_result->unsuccessful_authorize = 1;
				}
			}
			//else{
			// Put here some other authorization method if you need.
			// For example from some Single sign-on service and create new user if needed.
			//}
			
			
			// This will happen only once when user is logged in.
			// Put here things that you want to store in session or do
			// only when user is authorized.
			if($user->authorized){
				
				// set user groups
				//$user->set_groups();
				
				// save last login time
				$user->save_last_login_time();
				
				// serialize user object into session
				$user->session_save();
			}
		}
		
		// This will happen on every request after user is authorized.
		if($user->authorized){
			
			// put reference to user object into registry
			Psa_Registry::get_instance()->user = $user;
			
			// some values that will be available in all templates
			$this->psa_result->username = $user->username;
		}
		// user is not authorized
		else{
			// throw Unauthorized_Access_Exception
			include_once 'application/exceptions/Unauthorized_Access_Exception.php';
			throw new Unauthorized_Access_Exception("Login needed.", false);
		}
	}
}
